package important.demo;

import presence.demo.R;
import database.demo.ImportantOperations;
import database.demo.PassengersOperations;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


public class SpecificImportantMainActivity extends Activity {
	private ImportantOperations importantDBoperation;
	TextView tmpID;
	TextView tmpName;
	TextView tmpRole;
	TextView tmpPhone;
	TextView tmpEmail;
	TextView tmpAddress;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.specific_important);	
		importantDBoperation = new ImportantOperations(this);
		importantDBoperation.open();
		addButtonListeners();
		
		tmpID = (TextView) findViewById(R.id.spec_id);
		tmpName = (TextView) findViewById(R.id.dname_text);
		tmpRole = (TextView) findViewById(R.id.spec_role);
		tmpPhone = (TextView) findViewById(R.id.spec_phone);
		tmpEmail = (TextView) findViewById(R.id.spec_email);
		tmpAddress = (TextView) findViewById(R.id.important_address);
		
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
		    String value = extras.getString("tmpID");
		    tmpID.setText(value);
		    Cursor cursor = importantDBoperation.getImportantContact(value);
		    tmpName.setText(cursor.getString(1));
		    tmpRole.setText(cursor.getString(2));
		    tmpPhone.setText(cursor.getString(3));
		    tmpEmail.setText(cursor.getString(4));
		    tmpAddress.setText(cursor.getString(5));
		    
		}		
	}
	
	@Override
	protected void onResume() {
	   super.onResume();
	   this.onCreate(null);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.secific_pass, menu);
        return true;
    }
	
	public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.item1:
        {
        	Intent intent=new Intent(important.demo.SpecificImportantMainActivity.this,EditContactMainActivity.class);
        	intent.putExtra("ID", tmpID.getText());
        	startActivity(intent);
          return true;
        }
        case R.id.item2:
        	AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
     
    			// set title
    			alertDialogBuilder.setTitle("Delete Contact");
     
    			// set dialog message
    			alertDialogBuilder
    				.setMessage("delete "+tmpName.getText().toString()+"?")
    				.setCancelable(false)
    				.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
    					public void onClick(DialogInterface dialog,int id) {
    						// if this button is clicked delete record
    						
    						if(importantDBoperation.deleteImportant(tmpID.getText().toString()))
    				          {
    				        	  Toast.makeText(SpecificImportantMainActivity.this, tmpName.getText().toString()+" deleted!", Toast.LENGTH_LONG).show();
    				          }
    						SpecificImportantMainActivity.this.finish();
    					}
    				  })
    				.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
    					public void onClick(DialogInterface dialog,int id) {
    						// if this button is clicked, just close
    						// the dialog box and do nothing
    						dialog.cancel();
    					}
    				});
     
    				// create alert dialog
    				AlertDialog alertDialog = alertDialogBuilder.create();
    				// show it
    				alertDialog.show();
    				return true;
    			}
		return false;
    } 
	
	public void addButtonListeners() {	 
		ImageButton dial_btn = (ImageButton) findViewById(R.id.btn_dial);
		ImageButton message_btn = (ImageButton) findViewById(R.id.btn_message);
		tmpPhone = (TextView) findViewById(R.id.spec_phone);
		dial_btn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View view) {
			if(tmpPhone.getText().toString().length()==0)
			{
				Toast.makeText(SpecificImportantMainActivity.this,"No specified number", Toast.LENGTH_LONG).show();
			}
			else 
            {
				String uri = "tel:" + tmpPhone.getText().toString().trim() ;
				Intent intent = new Intent(Intent.ACTION_CALL);
				intent.setData(Uri.parse(uri));
				startActivity(intent);
            }	
		}
		}); 	
		
		message_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if(tmpPhone.getText().toString().length()==0)
				{
					//Toast.makeText(SpecificPassMainActivity.this,"No specified number", Toast.LENGTH_LONG).show();
				}
				else 
	            {
					// add the phone number in the data
					Uri uri = Uri.parse("smsto:" + tmpPhone.getText().toString());
					
					Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
					// add the message at the sms_body extra field
					//smsSIntent.putExtra("sms_body", smsBody.getText().toString());
					try{
						startActivity(smsSIntent);
					} catch (Exception ex) {
						//Toast.makeText(SpecificPassMainActivity.this, "Your sms has failed...",Toast.LENGTH_LONG).show();
						ex.printStackTrace();
					}
					 
	            }	
			}
			});
	}
	
		
}








